package th.kmutt.se.is.fact.adapter;

import java.text.DecimalFormat;
import th.kmutt.se.is.R;
import th.kmutt.se.is.model.Room;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RoomAdapter extends ArrayAdapter<Room> {

	Context context;
	int layoutResourceId;
	Room data[] = null;

	public RoomAdapter(Context context, int layoutResourceId, Room[] data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView; 
		Holder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new Holder();
			holder.roomRow = (RelativeLayout)row.findViewById(R.id.roomRow);
			holder.roomName = (TextView) row.findViewById(R.id.roomName);
			holder.roomFloor = (TextView) row.findViewById(R.id.roomFloor);
			holder.roomDesc = (TextView) row.findViewById(R.id.roomDesc);
			holder.roomStatus = (Button) row.findViewById(R.id.roomStatus);

			row.setTag(holder);
		} else {
			holder = (Holder) row.getTag();
		}

		Room room = data[position];
		
		Log.i("roomStatus", room.getStatus()+"");
		
		holder.roomName.setText(room.getName());
		
		if(room.getFloor()!=0)holder.roomFloor.setText(context.getString(R.string.fl)+room.getFloor());
		else holder.roomFloor.setText("");
		
		if(room.getStatus()==1){
			DecimalFormat df = new DecimalFormat("#,##0.00");
			holder.roomDesc.setText(df.format(room.getPrice())+"/"+context.getString(R.string.month));
			holder.roomStatus.setBackgroundColor(context.getResources().getColor(R.color.color08));
		}else if(room.getStatus()==2){
			holder.roomStatus.setBackgroundColor(context.getResources().getColor(R.color.color10));
		}else{
			holder.roomStatus.setBackgroundColor(context.getResources().getColor(R.color.color12));
		}

		return row;
	}

	static class Holder {
		Button roomStatus;
		RelativeLayout roomRow;
		TextView roomName, roomFloor, roomDesc;
	}
}
